package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.urlrouter.UrlRouter;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.BannerLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.BannerItem;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.TopicBannerParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wcy on 18/5/24.
 */

public class AdSelectedTopicHeaderLayout extends LinearLayout {
    @BindView(R.id.banner)
    BannerLayout mBanner;
    @BindView(R.id.change)
    TextView mChange;
    @BindView(R.id.divider)
    View mDivider;
    @BindView(R.id.label)
    TextView mLabel;
    //public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    boolean needTrack = false;

    public AdSelectedTopicHeaderLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedTopicHeaderLayout(Context context, boolean needTrack) {
        super(context);
        init();
        this.needTrack = needTrack;
    }

    public AdSelectedTopicHeaderLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedTopicHeaderLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_topic_banner_layout, this);
        ButterKnife.bind(view);
    }

    public void refresh(String type, String lable1, String label2) {
        mLabel.setText(lable1);
        mChange.setText(label2);
        refresh(type);
    }

    public void refresh(String type) {
        BookCityPresenter presenter = new BookCityPresenter(null);
        TopicBannerParams params = new TopicBannerParams();
        params.type_name = type;
        params.num = 20;
        presenter.getTopicsBanner(params, new INetCommCallback<List<BannerItem>>() {
            @Override
            public void onResponse(List<BannerItem> topicBannerBeans) {
                Log.d("banner", "getTopicsBanner onResponse");
                if (Utils.isEmptyList(topicBannerBeans)) {
                    DisplayUtils.gone(mBanner, mDivider);
                    return;
                } else {
                    DisplayUtils.visible(mBanner, mDivider);
                }
                List<String> list = new ArrayList<>();
                for (BannerItem bean : topicBannerBeans) {
                    list.add(bean.img_url);
                }
                mBanner.setViewUrls(list, DisplayUtils.dip2px(getContext(), 22));
                mBanner.getPager().setPageMargin(DisplayUtils.dip2px(getContext(), 4));
                mBanner.getPager().invalidate();
                mBanner.setOnBannerItemClickListener(position -> {
                    if (position < topicBannerBeans.size()) {
                        UrlRouter.from(getContext()).jump(topicBannerBeans.get(position).jump_url);
                        if (needTrack && position < 5) {
                            AdPresenter.pvuvStaticsByString(getResources().getStringArray(R.array.FXBANNER_POSITION)[position]);
                        }
                    }
                });
            }

            @Override
            public void onError(int code, String errDesc) {
                DisplayUtils.gone(mBanner, mDivider);
            }
        });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //lifeCyclerSubject.onNext(ACTION_DESTORY);
    }

    @OnClick(R.id.change)
    public void onViewClicked() {
        ARouterUtils.toActivity(ARouterConfig.TOPIC_LIST_ALL);
    }
}
